package com.beaut.project.service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import yh.core.funcs.person.data.YHPerson;
import yh.core.global.YHConst;

import com.beaut.project.dao.CoreMapper;
import com.beaut.project.dao.FlowMapper;
import com.beaut.project.pojo.Menu1;
import com.beaut.project.pojo.Menu2;
import com.beaut.project.pojo.Menu3;
import com.beaut.project.util.Utility;
import com.google.gson.Gson;




@Transactional(rollbackFor = Exception.class)
@Service("coreService")
public class CoreService {
	@Autowired
	private CoreMapper coreMapper;
	@Autowired
	private FlowMapper flowMapper;
	public String doLogin(Map<String,Object> map,HttpServletRequest request){
		//1.根据用户名查询用户信息
		Map<String,Object> user = coreMapper.getUserByUserName(map);
		Map mapDept=flowMapper.getFinanceDeptByDeptId(user);
		if(mapDept !=null){
			user.put("financeDeptId", String.valueOf(mapDept.get("id")));
		}
		if(user != null){
			String password = String.valueOf(user.get("password"));
			if(password.equals(String.valueOf(map.get("password")))){
				setSession(user, request);
				return Utility.rtJson(Utility.RETURN_OK, "操作成功", null);
			}else{
				return Utility.rtJson(Utility.RETURN_ERROR, "用户名或密码输入有误", null);
			}
			
		}else{
			return Utility.rtJson(Utility.RETURN_ERROR, "该用户名不存在", null);
		}
	}
	
	/**
	 * 设置session
	 * @param map
	 * @param request
	 */
	@SuppressWarnings("unchecked")
	private void setSession(Map<String, Object> map, HttpServletRequest request) {
		HttpSession session = request.getSession(true);
		Map<String, Object> sessionMap = (Map<String, Object>) session.getAttribute("LOGIN_USER");
		if (sessionMap == null) {
			session.setAttribute("LOGIN_USER", map);
			this.setFlowSession(map, request);
		} else {
			 if (!sessionMap.get("username").toString().equals(map.get("username").toString())) {
				// 一个客户端只能同时登陆一个账户，如果登陆用户名不等，销毁session
				session.invalidate();
				setSession(map, request);
			}
		}
	}
	/**
	 * 设置session
	 * @param map
	 * @param request
	 */
	@SuppressWarnings("unchecked")
	private void setFlowSession(Map<String, Object> map, HttpServletRequest request) {
		HttpSession session = request.getSession(true);
		Map m=flowMapper.getPersonByUser(map);
		YHPerson person=new YHPerson();
		person.setSeqId(Integer.valueOf(m.get("SEQ_ID").toString()));
		person.setAddHome("");
		person.setUserName(String.valueOf(m.get("USER_NAME")));
		
		person.setDeptId(Integer.valueOf(m.get("DEPT_ID").toString()));
		person.setUserPriv(m.get("USER_PRIV").toString());
		person.setUserId(m.get("USER_ID").toString());
		session.setAttribute(YHConst.LOGIN_USER,person);
	}
	public String getShowMenu(HttpServletRequest request,Map<String,Object> map){
		Gson gson = new Gson();
//		if(com.beaut.project.util.Utility.cache.get(String.valueOf(map.get("id"))) != null){
//			com.beaut.project.util.Utility.cache.get(String.valueOf(map.get("id")));
//			return gson.toJson(com.beaut.project.util.Utility.cache.get(String.valueOf(map.get("id")))); 
//		}
		Map<String,Object> userPriv = coreMapper.getUserPriv(map);//当前用户的权限
		List<Menu1> listMenu = new ArrayList<Menu1>();
		List<Map<String,Object>> list1 = getMenu1List(userPriv);
		
		if(list1 != null && list1.size()>0){
			for(int i=0;i<list1.size();i++){
				Map<String,Object> menu1Map = list1.get(i);
				Menu1 menu1 = new Menu1();
				menu1.setId(String.valueOf(menu1Map.get("id")));
				if(menu1Map.get("collapsed") == null){
					menu1.setCollapsed(false);
				}else{
					menu1.setCollapsed(Boolean.parseBoolean(String.valueOf(menu1Map.get("collapsed"))));
				}
				if(menu1Map.get("home_page") != null){
					menu1.setHomePage(String.valueOf(menu1Map.get("home_page")));
				}
				Map<String,Object> queryMap = new HashMap<String,Object>();
				queryMap.put("type", "2");
				queryMap.put("parent_id", menu1Map.get("seq_id"));
				List<Menu2> menu = new ArrayList<Menu2>();
				List<Map<String,Object>> list2 = coreMapper.getShowMenu(queryMap);
				if(list2 != null && list2.size()>0){
					for(int j=0;j<list2.size();j++){
						Map<String,Object> menu2Map = list2.get(j);
						//判断当前的二级菜单是不是符合用户的权限设置
						if(hasPriv(menu2Map.get("seq_id"), userPriv)){
							Menu2 menu2 = new Menu2();
							menu2.setText(String.valueOf(menu2Map.get("text")));
							if(menu2Map.get("collapsed") == null){
								menu2.setCollapsed(false);
							}else{
								menu2.setCollapsed(Boolean.parseBoolean(String.valueOf(menu2Map.get("collapsed"))));
							}
							queryMap.put("type", "3");
							queryMap.put("parent_id", menu2Map.get("seq_id"));
							List<Menu3> items = new ArrayList<Menu3>();
							List<Map<String,Object>> list3 = coreMapper.getShowMenu(queryMap);
							if(list3 !=null && list3.size() > 0){
								for(int k=0;k<list3.size();k++){
									Map<String,Object> menu3Map = list3.get(k);
									if(hasPriv(menu3Map.get("seq_id"), userPriv)){
										Menu3 menu3 = new Menu3();
										menu3.setId(String.valueOf(menu3Map.get("id")));
										menu3.setText(String.valueOf(menu3Map.get("text")));
										menu3.setHref(request.getContextPath()+String.valueOf(menu3Map.get("href")));
										if(menu3Map.get("closeable") == null){
											menu3.setCloseable(true);
										}else{
											menu3.setCloseable(Boolean.parseBoolean(String.valueOf(menu3Map.get("closeable"))));
										}
										items.add(menu3);
									}
								}
							}
							menu2.setItems(items);
							menu.add(menu2);
						}
					}
				}
				menu1.setMenu(menu);
				listMenu.add(menu1);
			}
		}
		com.beaut.project.util.Utility.cache.put(String.valueOf(map.get("id")),listMenu);
		return gson.toJson(listMenu);
	}
	
	public String getShowGd(Map<String, Object> map){
		List<Map<String,Object>> listGd = coreMapper.getShowGd(map);//当前用户的权限
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(listGd));
		Utility.putSuccess(map);
		return sb.toString();
	}
	public String getShowXm(Map<String, Object> map){
		List<Map<String,Object>> listGd = coreMapper.getShowXm(map);//当前用户的权限
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(listGd));
		Utility.putSuccess(map);
		return sb.toString();
	}
	public String getMenu1(Map<String, Object> map){
		Map<String,Object> userPriv = coreMapper.getUserPriv(map);//当前用户的权限
		Gson gson = new Gson();
		return Utility.rtJson(Utility.RETURN_OK, "操作成功", gson.toJson(getMenu1List(userPriv)));
	}
	
	/**
	 * 根据用户角色获取第一级菜单
	 * @param map
	 * @return
	 */
	private List<Map<String,Object>> getMenu1List(Map<String,Object> userPriv){
		List<Map<String,Object>> bakList = new ArrayList<Map<String,Object>>();
		Map<String,Object> queryMap = new HashMap<String,Object>();
		queryMap.put("type","1");
		List<Map<String,Object>> list = coreMapper.getMenuByType(queryMap);
		
		if(list != null && list.size() > 0 && userPriv != null){
			for(int i=0;i<list.size();i++){
				if(hasPriv(list.get(i).get("seq_id"), userPriv)){
					bakList.add(list.get(i));
				}
			}
		}
		return bakList;
	}
	private boolean hasPriv(Object menu_id,Map<String,Object> priv){
				String menu =  priv.get("menu")==null?"":String.valueOf(priv.get("menu"));
				List<String> ids = Arrays.asList(menu.split(","));
				if(ids!=null && ids.size()>0){
					for(int j=0;j<ids.size();j++){
						Map<String,Object> queryMap = new HashMap<String,Object>();
						queryMap.put("tree_code",menu_id);
						queryMap.put("seq_id",ids.get(j));
						if(coreMapper.hasPriv(queryMap) > 0){
							return true;
						}
					}
		}
		return false;
	}
	public String getMenus(){
		List<Map<String,Object>> list = coreMapper.getMenus();
		Gson gson = new Gson();
		StringBuffer sb = new StringBuffer("");
		sb.append("{\"total\":"+list.size()+",");
		sb.append("\"rows\":");
		sb.append(gson.toJson(list));
		sb.append("}");
		return sb.toString();
	}
	public String addMenu(Map<String,Object> map){
		String seq_id = UUID.randomUUID().toString();
		map.put("seq_id", seq_id);
		if("1".equals(String.valueOf(map.get("type")))){
			map.put("tree_code", "0,"+seq_id);
		}else{
			Map<String,Object> parentMap = coreMapper.getMenuById(String.valueOf(map.get("parent_id")));
			if(parentMap != null){
				map.put("tree_code", parentMap.get("tree_code")+","+seq_id);
			}
		}
		coreMapper.addMenu(map);
		return Utility.rtJson(Utility.RETURN_OK, "操作成功", null);
	}
	public String updateMenu(Map<String,Object> map){
		coreMapper.updateMenu(map);
		return Utility.rtJson(Utility.RETURN_OK, "操作成功", null);
	}
	public String deleteMenu(String id){
		List<String> ids = coreMapper.getChildren(id);
		coreMapper.deleteMenu(ids);
		return Utility.rtJson(Utility.RETURN_OK, "操作成功", null);
	}
	public String getMenu(String id){
		Map<String,Object> map =  coreMapper.getMenuById(id);
		if("false".equals(String.valueOf(map.get("collapsed")))){
			map.put("collapsed", "0");
		}else{
			map.put("collapsed", "1");
		}
		if("false".equals(String.valueOf(map.get("closeable")))){
			map.put("closeable", "0");
		}else{
			map.put("closeable", "1");
		}
		return Utility.toJson(map);
	}
}
